﻿//-----------------------------------------------------------------------
// <copyright file="ProductExtensions.cs" company="Streetlight Technologies L.L.C.">
//    Copyright Streetlight Technologies L.L.C. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Streetlight.Store.DataAccess
{
    using System;
    using Streetlight.Store.Contracts;

    /// <summary>
    /// Provides static methods extending the DataModels.Product and Contracts.Product classes.
    /// </summary>
    internal static class ProductExtensions
    {
        /// <summary>
        /// Creates a new instance of Contracts.Product and copies values from specified DataModels.Product instance.
        /// </summary>
        /// <param name="productData">DataModels.Product containing data</param>
        /// <returns>New instance of Contracts.Product</returns>
        internal static Product CreateContract(this DataAccess.DataModels.Product productData)
        {
            Product product = new Product();

            CopyToContract(productData, product);

            return product;
        }

        /// <summary>
        /// Copies values from DataModels.Product to Contracts.Product.
        /// </summary>
        /// <param name="productData">DataModels.Product containing values to copy</param>
        /// <param name="product">Contracts.Product to copy to</param>
        internal static void CopyToContract(this DataAccess.DataModels.Product productData, Product product)
        {
            product.Id = Convert.ToString(productData.ProductId);
            product.ItemNumber = productData.ItemNumber;
            product.GlobalId = productData.GlobalId;
            product.Name = productData.Name;
            product.Manufacturer = productData.Manufacturer;
            product.PartNumber = productData.PartNumber;
            product.Description = productData.Description;
            product.Price = productData.Price;
            product.Length = productData.Length;
            product.Width = productData.Width;
            product.Height = productData.Height;
            product.Weight = productData.Weight;
            product.PackagingTypeCode = productData.PackagingTypeCode;

            if (productData.StatusId.HasValue)
            {
                product.Status = Convert.ToString((ProductStatuses)productData.StatusId);
            }
        }

        /// <summary>
        /// Creates a new instance of DataModels.Product and copies values from specified Contracts.Product instance.
        /// </summary>
        /// <param name="product">Contracts.Product containing data</param>
        /// <returns>New instance of DataModels.Product</returns>
        internal static DataAccess.DataModels.Product CreateEntity(this Product product)
        {
            DataModels.Product productData = new DataModels.Product();

            CopyToEntity(product, productData);

            return productData;
        }

        /// <summary>
        /// Copies values from Contracts.Product to DataModels.Product.
        /// </summary>
        /// <param name="product">Contracts.Product containing values to copy</param>
        /// <param name="productData">DataModels.Product to copy to</param>
        internal static void CopyToEntity(this Product product, DataAccess.DataModels.Product productData)
        {
            productData.ItemNumber = product.ItemNumber;
            productData.GlobalId = product.GlobalId;
            productData.Name = product.Name;
            productData.Manufacturer = product.Manufacturer;
            productData.PartNumber = product.PartNumber;
            productData.Description = product.Description;
            productData.Price = product.Price;
            productData.Length = product.Length;
            productData.Width = product.Width;
            productData.Height = product.Height;
            productData.Weight = product.Weight;
            productData.PackagingTypeCode = product.PackagingTypeCode;

            if (!string.IsNullOrEmpty(product.Status))
            {
                productData.StatusId = (byte)Enum.Parse(typeof(ProductStatuses), product.Status);
            }
        }
    }
}
